<?php
namespace app\admin\validate;
use think\Validate;
use app\admin\model\AdminUser as User;
class AdminUser extends Validate
{

// 正则列
	protected $regexv =
	[
		'idcheck'=> '^[123456789]$|^[123456789][0-9]{0,3}$',
		'switch' => '[0]|[1]',
		'number'=>'^[123456789]$|^[123456789][\d]{0,10}$',
		'phone'=> '0?1[3456789][0-9]{9}',
		'switch1' => '(open)|(shut)',
	];

	// 规则列
	protected $rule =
	[
		'id'			=>	'require|regex:idcheck',
		'group_id'		=>	'require|regex:idcheck',
		'username'		=>	'require|alphaNum|length:5,24',
		'password'		=>	'require|length:5,24',
		'repassword'	=>	'require|length:5,24|confirm:password',
		'name'			=>	'require|chsDash',
		'phone'			=>	'require|regex:phone',
		'email'			=>	'require|email',
		'status'		=>	'require|switch',
		'note'			=>	'require|chsDash|length:5,24',
		'nuptial'		=>	'require|switch1|checkNuptialDb',
	];

	// 消息列
	protected $message =
	[
		'id.require'			=>	'ID_Must_Input',
		'id.regex'				=>	'ID_Format_Error',

		'group_id.require'		=>	'Group_id_Must_Input',
		'group_id.regex'		=>	'Group_id_Format_Error',

		'username.require'		=>	'User_Must_Input',
		'username.alphaNum'		=>	'User_Format_Error',
		'username.length'		=>	'User_Length_Beyond',

		'password.require'		=>	'Password_Must_Input',
		'password.length'		=>	'Password_Length_Beyond',

		'repassword.require'	=>	'Password_Must_Input',
		'repassword.length'		=>	'Password_Length_Beyond',
		'repassword.confirm'	=>	'Password_Length_Confirm',

		'name.require'			=>	'Name_Must_Input',
		'name.alphaNum'			=>	'Name_Format_Error',

		'phone.require'			=>	'Phone_Must_Input',
		'phone.regex'			=>	'Phone_Format_Error',

		'email.require'			=>	'Email_Must_Input',
		'email.email'			=>	'Email_Format_Error',

		'status.require'		=>	'Status_Must_Input',
		'status.regex'			=>	'Status_Format_Error',

		'note.require'			=>	'Note_Must_Input',
		'note.length'			=>	'Length_Not_Range',

		'nuptial.require'		=>	'Nuptial_Must_Input',
		'nuptial.regex'			=>	'Nuptial_Not_Range',
		'nuptial.checkNuptialDb'=>	'User_Check_Nuptial',
	];

	// 场景列
	protected $scene =
	[
		'useradd'		=>	['group_id','username','password','repassword','name','phone','email','note','nuptial'],
		'useredit'		=>	['id','group_id','name','phone','email','note'],
		'userid'		=>	['id'],
	];

	/**
	 * checkAdminDb 进行用户检验数据检测
	 * @param  string $value 传入检验的字符
	 * @param  string $rule  传入检测的规则
	 * @param  array $data  整个数据内容传入
	 * @return string        错误时返回一个字符串
	 * @return bool        正确时返回一个bool的真值
	 */
	protected function checkNuptialDb($value,$rule,$data=[])
	{
		// 开关是否被打开
		if ($value === 'shut') return true;
		else
		{
			$num = User::where('username', $data['username'])->count();
			if (0 < $num) return 'User_Already_Registered';
			else return true;
		}
	}
}